package com.example.projectcode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.projectcode.mapper.ChapterMapper;
import com.example.projectcode.mapper.ComprehensiveMapper;
import com.example.projectcode.mapper.TestPointMapper;
import com.example.projectcode.model.Chapter;
import com.example.projectcode.model.Choice;
import com.example.projectcode.model.Comprehensive;
import com.example.projectcode.model.Testpoint;
import com.example.projectcode.service.ComprehensiveService;
import com.example.projectcode.vo.ComprehensiveVo;
import com.example.projectcode.vo.UpdateComprehensiveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Service
public class ComprehensiveServiceImpl extends ServiceImpl<ComprehensiveMapper,Comprehensive> implements ComprehensiveService {

  @Autowired
  private ComprehensiveMapper comprehensiveMapper;

  @Autowired
  private TestPointMapper testPointMapper;

  @Autowired
  private ChapterMapper chapterMapper;

    @Override
    public List<Comprehensive> selectAllComprehensive(Integer testPointid, String questionCategory) {

//        QueryWrapper<Comprehensive> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("Testpointid",testPointid).eq("Cp_type",questionCategory);
    List<Comprehensive> comprehensiveList = new ArrayList<>();
    comprehensiveList = comprehensiveMapper.selectAllComprehensives(testPointid,questionCategory);
        return comprehensiveList;
    }

  @Override
  public Comprehensive selectComprehensive(Integer testPointid, String questionCategory, Integer order) {

      Comprehensive comprehensive = comprehensiveMapper.selectComprehensive(testPointid,questionCategory,order);
      return comprehensive;
  }

  @Override
  public int countComprehensive(Integer TestPointid,String questionCategory) {
      List<Comprehensive> comprehensiveList = new ArrayList<>();
      comprehensiveList = comprehensiveMapper.selectAllComprehensives(TestPointid, questionCategory);
    Stream<Comprehensive> comprehensiveStream = comprehensiveList.stream();
    int count =  (int) comprehensiveStream.count();
    return count;
  }

    @Override
    public Comprehensive selectComprehensiveByCpId(Integer cpId) {


        return baseMapper.selectOne(new QueryWrapper<Comprehensive>().eq("Cp_id",cpId));
    }

    @Override
    public int insertComprehensive(ComprehensiveVo comprehensiveVo) {

        //根据考点名查询考点Id
        Testpoint testpoint = new Testpoint();
        QueryWrapper<Testpoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Testpointname",comprehensiveVo.getTestPointName());
        testpoint = testPointMapper.selectOne(queryWrapper);

        int testPointId = testpoint.getTestpointid();
        int chapterId = Integer.parseInt(testpoint.getChapid());

        //根据章节id查询章节
        Chapter chapter = new Chapter();
        QueryWrapper<Chapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("Chapid",chapterId);
        chapter  = chapterMapper.selectOne(queryWrapper1);


        int SubjectId = chapter.getSubid();

        Comprehensive comprehensive = new Comprehensive();

        comprehensive.setSubid(SubjectId);
        comprehensive.setChapid(chapterId);
        comprehensive.setTestpointid(testPointId);
        comprehensive.setCpStem(comprehensiveVo.getCpStem());
        comprehensive.setCpAnswer(comprehensiveVo.getCpAnswer());
        comprehensive.setCpType(comprehensiveVo.getCpType());
        comprehensive.setCpGraphlink(comprehensiveVo.getCpGraphlink());
        comprehensive.setCpVideolink(comprehensiveVo.getCpVideolink());

        return   comprehensiveMapper.insert(comprehensive);
    }

    @Override
    public int updateComprehensiveById(UpdateComprehensiveVo updateComprehensiveVo) {

       return comprehensiveMapper.updateComprehensiveById(updateComprehensiveVo);

    }

    @Override
    public int deleteComprehensiveById(Integer comprehensiveId) {
        return baseMapper.delete(new QueryWrapper<Comprehensive>().eq("Cp_id",comprehensiveId));
    }

    @Override
    public List<Comprehensive> selectComprehensiveByTestPointId(Integer testPointId) {
        return baseMapper.selectList(new QueryWrapper<Comprehensive>().eq("Testpointid",testPointId));
    }


}
